package com.parking.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.parking.common.core.model.PageInfo;
import com.parking.common.core.model.StateData;
import com.parking.order.model.PlatformMoney;
import com.parking.order.dao.PlatformMoneyDao;
import com.parking.order.model.dto.MoneyDto;
import com.parking.order.model.param.MoneyPlatformParam;
import com.parking.order.model.exception.OrderException;
import com.parking.order.model.exception.OrderExceptionCode;
import com.parking.order.service.IPlatformMoneyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.order.util.BasicInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Objects;

/**
 * <p>
 *  平台流水查询服务层
 * </p>
 *
 * @author 吴彦祖
 * @since 2023年11月28日
 */
@Service
public class PlatformMoneyServiceImpl extends ServiceImpl<PlatformMoneyDao, PlatformMoney> implements IPlatformMoneyService {
    /**
     * 平台流水数据层
     * */
    @Autowired
    private PlatformMoneyDao platformMoneyDao;
    /**
     * 其他模块调用工具类
     * */
    @Autowired
    private BasicInfoUtil basicInfoUtil;
    /**
     * 平台查询流水
     * 参数: 平台Id platformId
     *      开始时间 startTime
     *      结束时间 endTime
     *      订单编号 orderInfoNum
     *      流水类型 moneyType 0普通 1取消 2超时
     *      页数 pageNum
     *      每页大小 pegeSize
     * 结果: 订单编号 orderInfoNum
     *      流水类型 moneyType 普通/取消/超时
     *      支付时间 addTime
     *      流水金额 moneyNum
     * */
    @Override
    public PageInfo findMoneyPlatform(MoneyPlatformParam param) throws Exception {
        basicInfoUtil.getPlatform(param.getPlatformId());

        boolean isStartTime = param.getStartTime()!=null;
        boolean isEndTime = param.getEndTime()!=null;
        boolean isOrderInfoNum = param.getOrderInfoNum()!=null;
        boolean isMoneyType = param.getMoneyType()!=null;
        if(isStartTime&&isEndTime&&param.getEndTime().isBefore(param.getStartTime())){
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_START_NOT_BEFORE_END);
        }

        Page<PlatformMoney> page = new Page<>(param.getPageNum(), param.getPageSize());
        QueryWrapper<PlatformMoney> wrapper = new QueryWrapper<>();

        if(isStartTime) wrapper.ge("add_time",param.getStartTime());
        if(isEndTime) wrapper.le("add_time",param.getEndTime());
        if(isOrderInfoNum) wrapper.eq("order_info_num",param.getOrderInfoNum());
        if(isMoneyType) wrapper.eq("money_type",param.getMoneyType());
        wrapper.orderByDesc("add_time");

        platformMoneyDao.selectPage(page,wrapper);


        ArrayList<MoneyDto> dtos = new ArrayList<>();
        for (PlatformMoney money : page.getRecords()) {
            MoneyDto dto = BeanUtil.toBean(money, MoneyDto.class);
            String moneyType = null;
            if(Objects.equals(money.getMoneyType(), StateData.ORDER_MONEY_TYPE_ADD_ORDER)) moneyType = "普通";
            if(Objects.equals(money.getMoneyType(), StateData.ORDER_MONEY_TYPE_DELETE_ORDER)) moneyType = "取消";
            if(Objects.equals(money.getMoneyType(), StateData.ORDER_MONEY_TYPE_OVERTIME_ORDER)) moneyType = "超时";
            dto.setMoneyType(moneyType);
            dtos.add(dto);
        }

        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal(page.getTotal());
        pageInfo.setRecords(dtos);
        return pageInfo;
    }
}
